var WebSocket  = require('ws');
var fs         = require("fs");
var mysql      = require('mysql');
var imgpath    = '/data/tfintegrated/img/';
var urlpath    = 'http://121.36.195.22/';

var connection = mysql.createPool({
    host: '159.138.2.235',
    user: 'test235',
    password: 'wigoal@2020',
    port:  '3306',
    database: 'tfintegrated'
});

var  wswaring = new WebSocket('ws://121.36.195.22:8088/v2/subscription/warningResults?chns=0,1&fullpic=true');
//重连socket
function recontectwaringWebSocket()
{
    wswaring = new WebSocket('ws://121.36.195.22:8088/v2/subscription/warningResults?chns=0,1&fullpic=true');
    wswaring.on('open', function open()
    {
        exports.do = function (sql, callback)
        {
            this.getConnection(function (err, connection)
            {
                connection.query(sql, function ()
                {
                    callback.apply(connection, arguments);
                    connection.release();
                });
            })
        }.bind(connection)
    });

    wswaring.on('message', function incoming(data)
    {
        parsewaringMessgedata(data);
    });

    wswaring.on('close', function close()
    {
        console.log('close wswaring recontectWebSocket');
        setTimeout(function()
        {
            recontectwaringWebSocket();
        }, 30*1000);
    });
    wswaring.on('error', function error()
    {
        console.log("wswaring Error: recontectWebSocket");
    });
}

wswaring.on('open', function open()
{
    exports.do = function (sql, callback)
    {
        this.getConnection(function (err, connection)
        {
            connection.query(sql, function ()
            {
                callback.apply(connection, arguments);
                connection.release();
            });
        })
    }.bind(connection)
});

wswaring.on('message', function incoming(data)
{
    parsewaringMessgedata(data);
});

wswaring.on('close', function close()
{
    console.log('close wswaring');
    setTimeout(function()
    {
        recontectwaringWebSocket();
    }, 30*1000);
});

wswaring.on('error', function error()
{
    console.log("wswaring Error:");
});

//解析订阅预警结构化数据
function parsewaringMessgedata(data)
{
    var headlen = data.readInt32BE(0);
    var headval = data.toString('utf8',4,headlen+4);
    var jsonhead = JSON.parse(headval);

    var Channel = jsonhead.Channel;
    var ResultLen = jsonhead.ResultLen;
    var ObjPictureLen = jsonhead.ObjPictureLen;
    var FullPictureLen = jsonhead.FullPictureLen;
    var ResultType = jsonhead.ResultType;
    var WarnPictureLen = jsonhead.WarnPictureLen;
    var WarningInfoLen = jsonhead.WarningInfoLen;
    var WarningType = jsonhead.WarningType;
    var ObjPicturePath = null;
    var FullPicturePath = null;
    var WaringPicturePath = null;
    console.log('war');
    console.log(ResultType);
    var Resultval = data.toString('utf8',headlen+4,ResultLen+headlen+4);
    if (ObjPictureLen!=null && typeof(ObjPictureLen)!="undefined" && ObjPictureLen!=0)
    {
        var buff = Buffer.alloc(ObjPictureLen);
        for (var i = 0; i < buff.length; i++)
        {
            buff[i] = data[headlen+4+ResultLen+i];
        }
        var  curnowtimes = Date.now();
        var  tmpfilename = 'WAROBJP'+curnowtimes+'.jpg';
        var  tmppath = imgpath + tmpfilename;
        fs.writeFile(tmppath, buff,  function(err)
        {
            if (err)
            {
                ObjPicturePath = null;
            }
        });
        ObjPicturePath = urlpath + tmpfilename;
    }
    if (FullPictureLen!=null && typeof(FullPictureLen)!="undefined" && FullPictureLen!=0)
    {
        var buff = Buffer.alloc(FullPictureLen);
        for (var i = 0; i < buff.length; i++)
        {
            buff[i] = data[headlen+4+ResultLen+ObjPictureLen+i];
        }
        var  curnowtimes = Date.now();
        var  tmpfilename = 'WARFULLP'+curnowtimes+'.jpg';
        var  tmppath = imgpath + tmpfilename;
        fs.writeFile(tmppath, buff,  function(err)
        {
            if (err)
            {
                FullPicturePath = null;
            }
        });
        FullPicturePath = urlpath + tmpfilename;
    }
    var WarningInfoVal = data.toString('utf8',headlen+4+ResultLen+ObjPictureLen+FullPictureLen,headlen+4+ResultLen+ObjPictureLen+FullPictureLen+WarningInfoLen);
    if (WarnPictureLen!=null && typeof(WarnPictureLen)!="undefined" && WarnPictureLen!=0)
    {
        var buff = Buffer.alloc(WarnPictureLen);
        for (var i = 0; i < buff.length; i++)
        {
            buff[i] = data[headlen+4+ResultLen+ObjPictureLen+FullPictureLen+WarningInfoLen+i];
        }
        var  curnowtimes = Date.now();
        var  tmpfilename = 'WARPICALL'+curnowtimes+'.jpg';
        var  tmppath = imgpath + tmpfilename;
        fs.writeFile(tmppath, buff,  function(err)
        {
            if (err)
            {
                WaringPicturePath = null;
            }
        });
        WaringPicturePath = urlpath + tmpfilename;
    }
    if(ResultType==1)
    {
        parseMessgedatavehicleinfo(Resultval,ObjPicturePath,FullPicturePath,2);
    }
    else if(ResultType==2)
    {
        parseMessgedatatricyclesinfo(Resultval,ObjPicturePath,FullPicturePath,2);
    }
    else if(ResultType==3)
    {
        parseMessgedatapedestriansinfo(Resultval,ObjPicturePath,FullPicturePath,2);
    }
    else if(ResultType==4)
    {
        parseMessgedatabikesinfo(Resultval,ObjPicturePath,FullPicturePath,2);
    }
    else if(ResultType==5)
    {
        parseMessgedataperson(Resultval,ObjPicturePath,FullPicturePath,2);
    }
    parseWaringinfoMessgedata(WarningInfoVal,WaringPicturePath,WarningType);
}
//解析预警结构化数据
function parseWaringinfoMessgedata(WarningInfoVal,WaringPicturePath,WarningType)
{
    console.log('WarningType');
    console.log(WarningType);
    if(WarningType==1)
    {//ok
        parseWaringVehStrDesc_st(WarningInfoVal,WaringPicturePath);
    }
    else if(WarningType==2)
    {//ok
        parseWaringVehPicWarnDbt(WarningInfoVal,WaringPicturePath);
    }
    else if(WarningType==3)
    {

    }
    else if(WarningType==4)
    {
        parseWaringTriPicWarnDbt(WarningInfoVal,WaringPicturePath);
    }
    else if(WarningType==5)
    {
        parseWaringPedStrWarning_st(WarningInfoVal,WaringPicturePath);
    }
    else if(WarningType==6)
    {
        parseWaringPedPicWarnDbt(WarningInfoVal,WaringPicturePath);
    }
    else if(WarningType==7)
    {

    }
    else if(WarningType==8)
    {

    }
    else if(WarningType==9)
    {
        parseWaringFacePicWarnDbt(WarningInfoVal,WaringPicturePath);
    }
    else if(WarningType==10)
    {//ok
        parseWaringFaceLibInfoDbt(WarningInfoVal,WaringPicturePath);
    }
    else if(WarningType==11)
    {//ok
        parseWaringFaceStrWarnDbt(WarningInfoVal,WaringPicturePath);
    }
}

//解析预警结构化数据车辆图片布控
function parseWaringVehPicWarnDbt(WarningInfoVal,WaringPicturePath)
{
    if (WarningInfoVal!=null && typeof(WarningInfoVal)!="undefined" && WarningInfoVal!=0)
    {
        var Resultvaljson = JSON.parse(WarningInfoVal);
        var Desc = Resultvaljson.Desc;
        var Id = Resultvaljson.Id;
        var Name = Resultvaljson.Name;
        var Reason = Resultvaljson.Reason;
        var Similarity = Resultvaljson.Similarity;
        var Sumsquares = Resultvaljson.Sumsquares;
        var Threshold = Resultvaljson.Threshold;
        var Time = Resultvaljson.Time;
        var  sqlstr = 'INSERT INTO warning_result_vehpicwarndbt (`desc`,uid,name,reason,similarity,sumsquares,threshold,time'+') VALUES (\'' + Desc.toString() + '\',' + Id + ',\'' + Name.toString()
            + '\',\'' + Reason.toString() + '\',' + Similarity + ',' + Sumsquares
            + ',' + Threshold+ ',\'' + Time.toString()
            + '\')';
        //console.log(sqlstr);
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}
//解析预警结构化数据车辆属性布控
function parseWaringVehStrDesc_st(WarningInfoVal,WaringPicturePath)
{
    if (WarningInfoVal!=null && typeof(WarningInfoVal)!="undefined" && WarningInfoVal!=0)
    {
        var Resultvaljson = JSON.parse(WarningInfoVal);
        var Licence = Resultvaljson.Licence;
        var Id = Resultvaljson.Id;
        var Name = Resultvaljson.Name;
        var Reason = Resultvaljson.Reason;
        var VehStrDesc = Resultvaljson.VehStrDesc;
        var Time = Resultvaljson.Time;
        var  sqlstr = 'INSERT INTO warning_result_vehstrwarningst (licence,uid,name,reason,vehstrdesc,time'+') VALUES (\'' + Licence.toString() + '\',' + Id + ',\'' + Name.toString()
            + '\',\'' + Reason.toString() + '\',\'' + JSON.stringify(VehStrDesc) + '\',\'' + Time.toString()
            + '\')';
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}
//解析预警结构化数据行人属性布控
function parseWaringPedStrWarning_st(WarningInfoVal,WaringPicturePath)
{
    if (WarningInfoVal!=null && typeof(WarningInfoVal)!="undefined" && WarningInfoVal!=0)
    {
        var Resultvaljson = JSON.parse(WarningInfoVal);
        var Id = Resultvaljson.Id;
        var Name = Resultvaljson.Name;
        var PedStrDesc = Resultvaljson.PedStrDesc;
        var Reason = Resultvaljson.Reason;
        var Time = Resultvaljson.Time;
        var  sqlstr = 'INSERT INTO warning_result_pedstrwarningst (uid,name,pedstrdesc,reason,time'+') VALUES ('
            + Id +',\'' + Name.toString()
            + '\',\'' + JSON.stringify(PedStrDesc) + '\',\'' + Reason.toString() + '\',\'' + Time.toString()
            + '\')';
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}
//解析预警结构化数据行人图片布控
function parseWaringPedPicWarnDbt(WarningInfoVal,WaringPicturePath)
{
    if (WarningInfoVal!=null && typeof(WarningInfoVal)!="undefined" && WarningInfoVal!=0)
    {
        var Resultvaljson = JSON.parse(WarningInfoVal);
        var Desc = Resultvaljson.Desc;
        var Id = Resultvaljson.Id;
        var Name = Resultvaljson.Name;
        var Reason = Resultvaljson.Reason;
        var Similarity = Resultvaljson.Similarity;
        var Sumsquares = Resultvaljson.Sumsquares;
        var Threshold = Resultvaljson.Threshold;
        var Time = Resultvaljson.Time;
        var  sqlstr = 'INSERT INTO warning_result_pedpicwarndbt (`desc`,uid,name,reason,similarity,sumsquares,threshold,time'+') VALUES (\''
            + Desc.toString() + '\',' + Id + ',\'' + Name.toString()
            + '\',\'' + Reason.toString() + '\',' + Similarity + ',' + Sumsquares
            + ',' + Threshold+ ',\'' + Time.toString()
            + '\')';
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}
//解析预警结构化数据人脸结构化布控-普通模式
function parseWaringFaceStrWarnDbt(WarningInfoVal,WaringPicturePath)
{
    if (WarningInfoVal!=null && typeof(WarningInfoVal)!="undefined" && WarningInfoVal!=0)
    {
        var Resultvaljson = JSON.parse(WarningInfoVal);
        var FaceStrDesc = Resultvaljson.FaceStrDesc;
        var Reason = Resultvaljson.Reason;
        var Name = Resultvaljson.Name;
        var Time = Resultvaljson.Time;
        var Id = Resultvaljson.Id;
        var  sqlstr = 'INSERT INTO warning_result_facestrinfodbt (reason,uid,name,facestrdesc,time'+') VALUES (\'' + Reason.toString()+ '\','
             + Id + ',\'' + Name.toString()
            + '\',\'' +  JSON.stringify(FaceStrDesc)+ '\',\'' + Time.toString()
            + '\')';
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}

//解析预警结构化数据人脸陌生人模式
function parseWaringFaceLibInfoDbt(WarningInfoVal,WaringPicturePath)
{
    if (WarningInfoVal!=null && typeof(WarningInfoVal)!="undefined" && WarningInfoVal!=0) {
        var Resultvaljson = JSON.parse(WarningInfoVal);
        var Enable = Resultvaljson.Enable;
        if (Enable == true)
        {
            Enable = 1;
        }
        else
        {
            Enable = 0;
        }
        var Id = Resultvaljson.Id;
        var Name = Resultvaljson.Name;
        var Time = Resultvaljson.Time;
        var Reason = Resultvaljson.Reason;
        var StrangerMode = Resultvaljson.StrangerMode;
        if (StrangerMode == true) {
            StrangerMode = 1;
        }
        else
        {
            StrangerMode = 0;
        }
        var TotalMem = Resultvaljson.TotalMem;
        var  sqlstr = 'INSERT INTO warning_result_facelibinfodbt (enable,uid,name,reason,strangermode,totalmem,time'+') VALUES (' + Enable + ',' + Id + ',\'' + Name.toString()
            + '\',\'' + Reason.toString() + '\',' + StrangerMode + ',' + TotalMem
            + ',\''  + Time.toString()
            + '\')';
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}
//解析预警结构化数据人脸图片布控-普通模式
function parseWaringFacePicWarnDbt(WarningInfoVal,WaringPicturePath)
{
    if (WarningInfoVal!=null && typeof(WarningInfoVal)!="undefined" && WarningInfoVal!=0)
    {
        var Resultvaljson = JSON.parse(WarningInfoVal);
        var Address = Resultvaljson.Address;
        var Birthday = Resultvaljson.Birthday;
        var IDNumber = Resultvaljson.IDNumber;
        var LibId = Resultvaljson.LibId;
        var Id = Resultvaljson.Id;
        var Name = Resultvaljson.Name;
        var Sex = Resultvaljson.Sex;
        var Similarity = Resultvaljson.Similarity;
        var Sumsquares = Resultvaljson.Sumsquares;
        var Threshold = Resultvaljson.Threshold;
        var Time = Resultvaljson.Time;
        var  sqlstr = 'INSERT INTO warning_result_facepicwarndbt (address,birthday,idnumber,libid,uid,name,sex,similarity,sumsquares,threshold,time'+') VALUES (\'' + Address.toString()+ '\',\''
            + Birthday.toString()+ '\',\''+ IDNumber.toString()+ '\','+ LibId+ ',' + Id + ',\'' + Name.toString()
            + '\',' + Sex + ',' + Similarity + ',' + Sumsquares
            + ',' + Threshold+ ',\'' + Time.toString()
            + '\')';
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}

//解析预警结构化数据三轮车图片布控
function parseWaringTriPicWarnDbt(WarningInfoVal,WaringPicturePath)
{
    if (WarningInfoVal!=null && typeof(WarningInfoVal)!="undefined" && WarningInfoVal!=0)
    {
        var Resultvaljson = JSON.parse(WarningInfoVal);
        var Desc = Resultvaljson.Desc;
        var Id = Resultvaljson.Id;
        var Name = Resultvaljson.Name;
        var Reason = Resultvaljson.Reason;
        var Similarity = Resultvaljson.Similarity;
        var Sumsquares = Resultvaljson.Sumsquares;
        var Threshold = Resultvaljson.Threshold;
        var Time = Resultvaljson.Time;
        var  sqlstr = 'INSERT INTO warning_result_tripicwarndbt (desc,uid,name,reason,similarity,sumsquares,threshold,time'+') VALUES (\'' + Desc.toString() + '\',' + Id + ',\'' + Name.toString()
            + '\',\'' + Reason.toString() + '\',' + Similarity + ',' + Sumsquares
            + ',' + Threshold+ ',\'' + Time.toString()
            + '\')';
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}

//解析订阅结构化数据(机动车)
function parseMessgedatavehicleinfo(Resultval,objpath,fullpath,typeindex)
{
    if (Resultval!=null && typeof(Resultval)!="undefined" && Resultval!=0) {
        var Resultvaljson = JSON.parse(Resultval);
        var CarType = Resultvaljson.CarType;
        var Chn = Resultvaljson.Chn;
        var CoDriver = Resultvaljson.CoDriver;
        var CoNoBelt = Resultvaljson.CoNoBelt;
        var Color = Resultvaljson.Color;
        var DriveWay = Resultvaljson.DriveWay;
        var Drop = Resultvaljson.Drop;
        var HasCall = Resultvaljson.HasCall;
        var HasCrash = Resultvaljson.HasCrash;
        var HasRack = Resultvaljson.HasRack;
        var HasSpareTire = Resultvaljson.HasSpareTire;
        var HasSunroof = Resultvaljson.HasSunroof;
        var IsDanger = Resultvaljson.IsDanger;
        var IsSlag = Resultvaljson.IsSlag;
        var MainNoBelt = Resultvaljson.MainNoBelt;
        var MarkerNum = Resultvaljson.MarkerNum;
        var Orientation = Resultvaljson.Orientation;
        var Paper = Resultvaljson.Paper;
        var PlateInfo = Resultvaljson.PlateInfo;
        var Rect = Resultvaljson.Rect;
        var StyleName = Resultvaljson.StyleName;
        var Time = Resultvaljson.Time;
        var Type = Resultvaljson.Type;
        var sqlstr = null;
        if (typeindex == 1)
        {
            sqlstr = 'INSERT INTO event_result_vehicleinfo (cartype,chn,codriver,conobelt,color,driveway,`drop`,' +
                'hascall,hascrash,hasrack,hassparetire,hassunroof,isdanger,isslag,mainnobelt,markernum' +
                ',orientation,paper,plateinfo,rect,stylename,fullpicpath,objpicpath,time,type) VALUES (\'' + JSON.stringify(CarType)
                + '\',\'' + Chn.toString() + '\',\'' + JSON.stringify(CoDriver)
                + '\',\'' + JSON.stringify(CoNoBelt) + '\',\'' + JSON.stringify(Color)
                + '\',\'' + JSON.stringify(DriveWay) + '\',\'' + JSON.stringify(Drop)
                + '\',\'' + JSON.stringify(HasCall) + '\',\'' + JSON.stringify(HasCrash) + '\',\'' + JSON.stringify(HasRack)
                + '\',\'' + JSON.stringify(HasSpareTire) + '\',\'' + JSON.stringify(HasSunroof) + '\',\'' + JSON.stringify(IsDanger)
                + '\',\'' + JSON.stringify(IsSlag) + '\',\'' + JSON.stringify(MainNoBelt) + '\',\'' + JSON.stringify(MarkerNum)
                + '\',\'' + JSON.stringify(Orientation) + '\',\'' + JSON.stringify(Paper) + '\',\'' + JSON.stringify(PlateInfo) + '\',\'' + JSON.stringify(Rect)
                + '\',\'' + StyleName.toString() + '\',\'' + fullpath.toString() + '\',\'' + objpath.toString()
                + '\',\'' + Time.toString() + '\',\'' + JSON.stringify(Type)
                + '\')';
        }
        else if (typeindex == 2)
        {
            sqlstr = 'INSERT INTO warning_result_vehicleinfo (cartype,chn,codriver,conobelt,color,driveway,`drop`,' +
                'hascall,hascrash,hasrack,hassparetire,hassunroof,isdanger,isslag,mainnobelt,markernum' +
                ',orientation,paper,plateinfo,rect,stylename,fullpicpath,objpicpath,time,type) VALUES (\'' + JSON.stringify(CarType)
                + '\',\'' + Chn.toString() + '\',\'' + JSON.stringify(CoDriver)
                + '\',\'' + JSON.stringify(CoNoBelt) + '\',\'' + JSON.stringify(Color)
                + '\',\'' + JSON.stringify(DriveWay) + '\',\'' + JSON.stringify(Drop)
                + '\',\'' + JSON.stringify(HasCall) + '\',\'' + JSON.stringify(HasCrash) + '\',\'' + JSON.stringify(HasRack)
                + '\',\'' + JSON.stringify(HasSpareTire) + '\',\'' + JSON.stringify(HasSunroof) + '\',\'' + JSON.stringify(IsDanger)
                + '\',\'' + JSON.stringify(IsSlag) + '\',\'' + JSON.stringify(MainNoBelt) + '\',\'' + JSON.stringify(MarkerNum)
                + '\',\'' + JSON.stringify(Orientation) + '\',\'' + JSON.stringify(Paper) + '\',\'' + JSON.stringify(PlateInfo) + '\',\'' + JSON.stringify(Rect)
                + '\',\'' + StyleName.toString() + '\',\'' + fullpath.toString() + '\',\'' + objpath.toString()
                + '\',\'' + Time.toString() + '\',\'' + JSON.stringify(Type)
                + '\')';
        }
        //console.log(sqlstr);
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}
//解析订阅结构化数据(三轮车)
function parseMessgedatatricyclesinfo(Resultval,objpath,fullpath,typeindex)
{
    if (Resultval!=null && typeof(Resultval)!="undefined" && Resultval!=0)
    {
        var Resultvaljson = JSON.parse(Resultval);
        var Chn = Resultvaljson.Chn;
        var Convertible = Resultvaljson.Convertible;
        var DriveWay = Resultvaljson.DriveWay;
        var Manned = Resultvaljson.Manned;
        var Rect = Resultvaljson.Rect;
        var RoofColor = Resultvaljson.RoofColor;
        var Time = Resultvaljson.Time;
        var Type = Resultvaljson.Type;
        var sqlstr = null;
        if (typeindex == 1) {
        sqlstr = 'INSERT INTO event_result_tricyclesinfo (chn,convertible,driveway,fullpicpath,manned,objpicpath,' +
            'rect,roofcolor,time,type) VALUES (\''
            + Chn.toString() + '\',\'' + JSON.stringify(Convertible) + '\',\'' + JSON.stringify(DriveWay)
            + '\',\'' + fullpath.toString() + '\',\'' + JSON.stringify(Manned)
            + '\',\'' + objpath.toString() + '\',\'' + JSON.stringify(Rect) + '\',\'' + JSON.stringify(RoofColor)
            + '\',\'' + Time.toString() + '\',\'' + JSON.stringify(Type) + '\')';
        }
        else if (typeindex == 2) {
            sqlstr = 'INSERT INTO warning_result_tricyclesinfo (chn,convertible,driveway,fullpicpath,manned,objpicpath,' +
                'rect,roofcolor,time,type) VALUES (\''
                + Chn.toString() + '\',\'' + JSON.stringify(Convertible) + '\',\'' + JSON.stringify(DriveWay)
                + '\',\'' + fullpath.toString() + '\',\'' + JSON.stringify(Manned)
                + '\',\'' + objpath.toString() + '\',\'' + JSON.stringify(Rect) + '\',\'' + JSON.stringify(RoofColor)
                + '\',\'' + Time.toString() + '\',\'' + JSON.stringify(Type) +  '\')';
        }
        //console.log(sqlstr);
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}
//解析订阅结构化数据(非机动车)
function parseMessgedatabikesinfo(Resultval,objpath,fullpath,typeindex)
{
    if (Resultval!=null && typeof(Resultval)!="undefined" && Resultval!=0)
    {
        var Resultvaljson = JSON.parse(Resultval);
        var Age = Resultvaljson.Age; var Baby = Resultvaljson.Baby; var Bag = Resultvaljson.Bag;
        var BottomColor = Resultvaljson.BottomColor;var BottomType = Resultvaljson.BottomType;
        var Chn = Resultvaljson.Chn;var DriveWay = Resultvaljson.DriveWay;
        //var FullPicPath = Resultvaljson.FullPicPath;
        var Glasses = Resultvaljson.Glasses;var Hair = Resultvaljson.Hair;var Hat = Resultvaljson.Hat;
        var Knapsack = Resultvaljson.Knapsack;
        var Mask = Resultvaljson.Mask;var MessengerBag = Resultvaljson.MessengerBag;
        //var ObjPicPath = Resultvaljson.ObjPicPath;
        var Orientation = Resultvaljson.Orientation;
        var Rect = Resultvaljson.Rect;
        var Sex = Resultvaljson.Sex;var ShoulderBag = Resultvaljson.ShoulderBag;var Time = Resultvaljson.Time;
        var Type = Resultvaljson.Type; var Umbrella = Resultvaljson.Umbrella; var UpperColor = Resultvaljson.UpperColor;
        var UpperTexture = Resultvaljson.UpperTexture;
        var UpperType = Resultvaljson.UpperType;
        var sqlstr = null;
        if(typeindex==1)
        {
            sqlstr = 'INSERT INTO event_result_bikesinfo (age,baby,bag,bottomcolor,bottomtype,chn,driveway,fullpicpath,' +
                'glasses,hair,hat,knapsack,mask,messengerbag,objpicpath,' +
                'orientation,rect,sex,shoulderaag,time,type,umbrella,uppercolor,uppertexture,uppertype) VALUES (\''+ JSON.stringify(Age)
                + '\',\'' +  JSON.stringify(Baby)   + '\',\'' +  JSON.stringify(Bag)+ '\',\'' + JSON.stringify(BottomColor)+ '\',\'' + JSON.stringify(BottomType)
                + '\',\'' + Chn.toString()  + '\',\'' + JSON.stringify(DriveWay)  + '\',\'' + fullpath.toString()+
                '\',\'' + JSON.stringify(Glasses)
                + '\',\'' + JSON.stringify(Hair) + '\',\'' + JSON.stringify(Hat) + '\',\'' + JSON.stringify(Knapsack)+ '\',\'' + JSON.stringify(Mask)
                + '\',\'' + JSON.stringify(MessengerBag)+ '\',\'' + objpath.toString()
                + '\',\'' + JSON.stringify(Orientation) + '\',\'' + JSON.stringify(Rect)
                + '\',\'' + JSON.stringify(Sex) + '\',\'' + JSON.stringify(ShoulderBag)
                +'\',\'' + Time.toString() + '\',\'' + JSON.stringify(Type)
                + '\',\'' + JSON.stringify(Umbrella) + '\',\'' + JSON.stringify(UpperColor)
                + '\',\'' + JSON.stringify(UpperTexture) + '\',\'' + JSON.stringify(UpperType)
                +'\')';
        }
        else if(typeindex==2)
        {
            sqlstr = 'INSERT INTO warning_result_bikesinfo (age,baby,bag,bottomcolor,bottomtype,chn,driveway,fullpicpath,' +
                'glasses,hair,hat,knapsack,mask,messengerbag,objpicpath,' +
                'orientation,rect,sex,shoulderaag,time,type,umbrella,uppercolor,uppertexture,uppertype) VALUES (\''+ JSON.stringify(Age)
                + '\',\'' +  JSON.stringify(Baby)   + '\',\'' +  JSON.stringify(Bag)+ '\',\'' + JSON.stringify(BottomColor)+ '\',\'' + JSON.stringify(BottomType)
                + '\',\'' + Chn.toString()  + '\',\'' + JSON.stringify(DriveWay)  + '\',\'' + fullpath.toString()+
                '\',\'' + JSON.stringify(Glasses)
                + '\',\'' + JSON.stringify(Hair) + '\',\'' + JSON.stringify(Hat) + '\',\'' + JSON.stringify(Knapsack)+ '\',\'' + JSON.stringify(Mask)
                + '\',\'' + JSON.stringify(MessengerBag)+ '\',\'' + objpath.toString()
                + '\',\'' + JSON.stringify(Orientation) + '\',\'' + JSON.stringify(Rect)
                + '\',\'' + JSON.stringify(Sex) + '\',\'' + JSON.stringify(ShoulderBag)
                +'\',\'' + Time.toString() + '\',\'' + JSON.stringify(Type)
                + '\',\'' + JSON.stringify(Umbrella) + '\',\'' + JSON.stringify(UpperColor)
                + '\',\'' + JSON.stringify(UpperTexture) + '\',\'' + JSON.stringify(UpperType)
                +'\')';
        }
        //console.log(sqlstr);
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}
//解析订阅结构化数据(行人)
function parseMessgedatapedestriansinfo(Resultval,objpath,fullpath,typeindex)
{
    if (Resultval!=null && typeof(Resultval)!="undefined" && Resultval!=0) {
        var Resultvaljson = JSON.parse(Resultval);
        var Age = Resultvaljson.Age;
        var Baby = Resultvaljson.Baby;
        var Bag = Resultvaljson.Bag;
        var BottomColor = Resultvaljson.BottomColor;
        var BottomType = Resultvaljson.BottomType;
        var Chn = Resultvaljson.Chn;
        var DriveWay = Resultvaljson.DriveWay;
        //var FullPicPath = Resultvaljson.FullPicPath;
        var Glasses = Resultvaljson.Glasses;
        var Hair = Resultvaljson.Hair;
        var Hat = Resultvaljson.Hat;
        var Knapsack = Resultvaljson.Knapsack;
        var Mask = Resultvaljson.Mask;
        var MessengerBag = Resultvaljson.MessengerBag;
        //var ObjPicPath = Resultvaljson.ObjPicPath;
        var Orientation = Resultvaljson.Orientation;
        var Rect = Resultvaljson.Rect;
        var Sex = Resultvaljson.Sex;
        var ShoulderBag = Resultvaljson.ShoulderBag;
        var Time = Resultvaljson.Time;
        var Type = Resultvaljson.Type;
        var Umbrella = Resultvaljson.Umbrella;
        var UpperColor = Resultvaljson.UpperColor;
        var UpperTexture = Resultvaljson.UpperTexture;
        var UpperType = Resultvaljson.UpperType;
        var sqlstr = null;
        if (typeindex == 1)
        {
            sqlstr = 'INSERT INTO event_result_pedestriansinfo (age,baby,bag,bottomcolor,bottomtype,chn,driveway,fullpicpath,' +
                'glasses,hair,hat,knapsack,mask,messengerbag,objpicpath,' +
                'orientation,rect,sex,shoulderaag,time,type,umbrella,uppercolor,uppertexture,uppertype) VALUES (\'' + JSON.stringify(Age)
                + '\',\'' + JSON.stringify(Baby) + '\',\'' + JSON.stringify(Bag) + '\',\'' + JSON.stringify(BottomColor) + '\',\'' + JSON.stringify(BottomType)
                + '\',\'' + Chn.toString() + '\',\'' + JSON.stringify(DriveWay) + '\',\'' + fullpath.toString() +
                '\',\'' + JSON.stringify(Glasses)
                + '\',\'' + JSON.stringify(Hair) + '\',\'' + JSON.stringify(Hat) + '\',\'' + JSON.stringify(Knapsack) + '\',\'' + JSON.stringify(Mask)
                + '\',\'' + JSON.stringify(MessengerBag) + '\',\'' + objpath.toString()
                + '\',\'' + JSON.stringify(Orientation) + '\',\'' + JSON.stringify(Rect)
                + '\',\'' + JSON.stringify(Sex) + '\',\'' + JSON.stringify(ShoulderBag)
                + '\',\'' + Time.toString() + '\',\'' + JSON.stringify(Type)
                + '\',\'' + JSON.stringify(Umbrella) + '\',\'' + JSON.stringify(UpperColor)
                + '\',\'' + JSON.stringify(UpperTexture) + '\',\'' + JSON.stringify(UpperType)
                + '\')';
        }
        else if (typeindex == 2)
        {
                sqlstr = 'INSERT INTO warning_result_pedestriansinfo (age,baby,bag,bottomcolor,bottomtype,chn,driveway,fullpicpath,' +
                    'glasses,hair,hat,knapsack,mask,messengerbag,objpicpath,' +
                    'orientation,rect,sex,shoulderaag,time,type,umbrella,uppercolor,uppertexture,uppertype) VALUES (\'' + JSON.stringify(Age)
                    + '\',\'' + JSON.stringify(Baby) + '\',\'' + JSON.stringify(Bag) + '\',\'' + JSON.stringify(BottomColor) + '\',\'' + JSON.stringify(BottomType)
                    + '\',\'' + Chn.toString() + '\',\'' + JSON.stringify(DriveWay) + '\',\'' + fullpath.toString() +
                    '\',\'' + JSON.stringify(Glasses)
                    + '\',\'' + JSON.stringify(Hair) + '\',\'' + JSON.stringify(Hat) + '\',\'' + JSON.stringify(Knapsack) + '\',\'' + JSON.stringify(Mask)
                    + '\',\'' + JSON.stringify(MessengerBag) + '\',\'' + objpath.toString()
                    + '\',\'' + JSON.stringify(Orientation) + '\',\'' + JSON.stringify(Rect)
                    + '\',\'' + JSON.stringify(Sex) + '\',\'' + JSON.stringify(ShoulderBag)
                    + '\',\'' + Time.toString() + '\',\'' + JSON.stringify(Type)
                    + '\',\'' + JSON.stringify(Umbrella) + '\',\'' + JSON.stringify(UpperColor)
                    + '\',\'' + JSON.stringify(UpperTexture) + '\',\'' + JSON.stringify(UpperType)
                    + '\')';
        }
        //console.log(sqlstr);
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}
//解析订阅结构化数据(人脸)
function parseMessgedataperson(Resultval,objpath,fullpath,typeindex)
{
    if (Resultval!=null && typeof(Resultval)!="undefined" && Resultval!=0) {
        var Resultvaljson = JSON.parse(Resultval);
        var Age = Resultvaljson.Age;
        var Chn = Resultvaljson.Chn;
        //var FullPicPath = Resultvaljson.FullPicPath;
        var Glasses = Resultvaljson.Glasses;
        var Hair = Resultvaljson.Hair;
        var Hat = Resultvaljson.Hat;
        var Mask = Resultvaljson.Mask;
        //var ObjPicPath = Resultvaljson.ObjPicPath;
        var Rect = Resultvaljson.Rect;
        var RegionName = Resultvaljson.RegionName;
        var Sex = Resultvaljson.Sex;
        var Time = Resultvaljson.Time;
        var Type = Resultvaljson.Type;
        var sqlstr = null;
        if (typeindex == 1) {
        sqlstr = 'INSERT INTO event_result_person (age,chn,fullpicpath,glasses,hair,hat,mask,objpicpath,' +
            'rect,regionname,sex,time,type) VALUES (\'' + JSON.stringify(Age)
            + '\',\'' + Chn.toString() + '\',\'' + fullpath.toString() + '\',\'' + JSON.stringify(Glasses)
            + '\',\'' + JSON.stringify(Hair) + '\',\'' + JSON.stringify(Hat) + '\',\'' + JSON.stringify(Mask)
            + '\',\'' + objpath.toString() + '\',\'' + JSON.stringify(Rect) + '\',\'' + JSON.stringify(RegionName)
            + '\',\'' + JSON.stringify(Sex) + '\',\'' + Time.toString() + '\',\'' + JSON.stringify(Type)
            + '\')';
        }
        else if (typeindex == 2) {
            sqlstr = 'INSERT INTO warning_result_person (age,chn,fullpicpath,glasses,hair,hat,mask,objpicpath,' +
                'rect,regionname,sex,time,type) VALUES (\'' + JSON.stringify(Age)
                + '\',\'' + Chn.toString() + '\',\'' + fullpath.toString() + '\',\'' + JSON.stringify(Glasses)
                + '\',\'' + JSON.stringify(Hair) + '\',\'' + JSON.stringify(Hat) + '\',\'' + JSON.stringify(Mask)
                + '\',\'' + objpath.toString() + '\',\'' + JSON.stringify(Rect) + '\',\'' + JSON.stringify(RegionName)
                + '\',\'' + JSON.stringify(Sex) + '\',\'' + Time.toString() + '\',\'' + JSON.stringify(Type)
                + '\')';
        }
        //console.log(sqlstr);
        connection.query(sqlstr, function(err, results)
        {
        });
    }
}